/*
 * @Description: 
 * @Author: 张凌瑞
 * @Date: 2021-02-26 11:11:45
 * @LastEditTime: 2021-02-27 07:38:51
 * @LastEditors: Zhang Zhaoda
 */

//引入样式
// import {Iobj} from "@/interface/type.inter"
import {Iobj} from "../../interface/type.inter"
//引入类型
import {GETTYPEDATA,CHANGEKEY} from "../types/type.types"

export interface KeyInter{
  [key: string]: any
}
export interface Istate{
  typeData:KeyInter,
  typeKey:string,
}
let defaultData={
    typeData:{},//车型页面数据,
    typeKey:''
}
let reducer=(state:Istate=defaultData,action:any)=>{
  switch (action.type) {
    case GETTYPEDATA:{
      let {list} = action;
      let typeData: Iobj = {};
      list.forEach((item: any) => {
        if (!typeData[item.market_attribute.year]) {
          typeData[item.market_attribute.year] = {}
        }
      })
      list.forEach((item: any) => {
        if (typeData[item.market_attribute.year]) {
          if (!typeData[item.market_attribute.year][`${item.exhaust_str}/${item.max_power_str} ${item.inhale_type}`]) {
            typeData[item.market_attribute.year][`${item.exhaust_str}/${item.max_power_str} ${item.inhale_type}`] = []
            typeData[item.market_attribute.year][`${item.exhaust_str}/${item.max_power_str} ${item.inhale_type}`].push(item)
          } else {
            typeData[item.market_attribute.year][`${item.exhaust_str}/${item.max_power_str} ${item.inhale_type}`].push(item)
          }
        }
      })
      
      return {...state,typeData,typeKey:Object.keys(typeData)[0]}
    }
    
    case CHANGEKEY:{
      
      return {...state,typeKey:action.typeKey}
    }
      
  
    default:
     return state
  }
}


export default reducer